//  Created by <Omar MALAGOUEN>

//  Copyright 2011 All rights reserved.
//

#include "cquitdialog.h"
#include "ui_cquitdialog.h"

CQuitDialog::CQuitDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::CQuitDialog)
{
    ui->setupUi(this);
    qTimer = new QTimer;
    qTimer->start(1000);
    count = 0;
    max = 10;
    response = false;

    QString text = QString(tr("The application will close automatically in ")) + QString::number(max-count,10);
    text = text + QString(tr(" seconds."));
    ui->labelTitle->setText(text);
    ui->labelIcon->setPixmap(QPixmap(":/resources/icons/exit.png"));

    connect(qTimer,SIGNAL(timeout()), this, SLOT(setTimer()));
}

CQuitDialog::~CQuitDialog()
{
    delete ui;
}

void CQuitDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void CQuitDialog::setTimer(){
    count++;
    QString text = QString(tr("The application will close automatically in ")) + QString::number(max-count,10);
    text = text + QString(tr(" seconds."));
    ui->labelTitle->setText(text);
    qTimer->stop();
    qTimer->start(1000);

    if ( (max - count) == -1)
    {
        qTimer->stop();
        this->response = true;
        this->hide();
        this->close();
    }
    else {
        qTimer->stop();
        qTimer->start(1000);
    }
}

void CQuitDialog::on_pushButtonQuit_clicked()
{
    qTimer->stop();
    this->response = true;
    this->hide();
    this->close();
}

void CQuitDialog::on_pushButtonCancel_clicked()
{
    qTimer->stop();
    this->response = false;
    this->hide();
    this->close();
}
